<?php
namespace app\admin\controller;

use app\common\controller\Admin;

class Tag extends Admin
{
    //定义表单字段列表
    protected function fields($data=[])
    {
        return model('admin/Tag','loglic')->fields($data);
    }
    
    //定义表单初始数据
    protected function formData()
    {
        if( $id = input('id/d',0) ){
            return \daicuo\Term::get_id($id, false);
		}
        return [];
    }

    //定义表格数据（JSON）
    //tag/index/?pageNumber=1&pageSize=20&sortName=user_id&sortOrder=desc&term_tpl=index&term_tpl_=like
    protected function ajaxJson()
    {
        //查询参数
        $args = array();
        $args['cache']    = false;
        $args['type']     = 'tag';
        $args['limit']    = input('pageSize/d', 50);
        $args['page']     = input('pageNumber/d', 1);
        $args['sort']     = input('sortName/s','term_id');
        $args['order']    = input('sortOrder/s','desc');
        $args['search']   = input('searchText/s','');
        //基础字段查询条件
        if( $where = DcWhereQuery(['term_order','term_module','term_status'], 'eq', $this->query) ){
            $args['where']  = $where;
        }
        //扩展字段查询条件
        $args['meta_query'] = DcMetaQuery('term_meta', $this->query);
        //数据返回
        return DcEmpty(DcTermSelect($args),['total'=>0,'data'=>[]]);
    }
    
	//添加(数据库)
	public function save()
    {
        config('common.validate_name', 'common/Term');
        
        config('common.validate_scene', 'save');
        
        config('common.where_slug_unique', ['term_type'=>['eq','tag']]);
        
        $id = \daicuo\Term::save(input('post.'));
        
		if($id < 1){
			$this->error(\daicuo\Term::getError());
		}
        
		$this->success(lang('success'));
	}
    
    //删除(数据库)
	public function delete()
    {
		$ids = input('id/a');
		if(!$ids){
			$this->error(lang('errorIds'));
		}
        
        \daicuo\Term::delete_ids($ids);

        $this->success(lang('success'));
	}
    
    //修改（数据库）
	public function update()
    {
		$data = input('post.');
        if(!$data['term_id']){
            $this->error(lang('errorIds'));
        }
        
        config('common.validate_name', 'common/Term');
        
        config('common.validate_scene', 'update');
        
        config('common.where_slug_unique', ['term_type'=>['eq','tag']]);
        
        $info = \daicuo\Term::update_id($data['term_id'], $data);
        if(is_null($info)){
            $this->error(\daicuo\Term::getError());
        }
        $this->success(lang('success'));
	}
    
    //快速修改状态
    public function status()
    {
        if( !$ids = input('post.id/a') ){
            $this->error(lang('errorIds'));
        }
        $data = [];
        $data['term_status'] = input('request.value/s', 'hidden');
        foreach($ids as $key=>$id){
            \daicuo\Term::update(['term_id'=>$id], $data, '');
        }
        $this->success(lang('success'));
    }
}